package com.test;

/**
 * Created by 41579 on 2017/4/16.
 */
public class SyncObj {
    Integer lock = 1;
    Integer lock2 = 2;

    public synchronized void execute() {
        try {
            System.out.println(Thread.currentThread().getName() + "：begin....");
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + "：syncRunnable....");
    }

    public void execute2() {
        synchronized (lock) {
            System.out.println(Thread.currentThread().getName() + "：begin....");
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "：syncRunnable2....");
        }
    }

    public void execute3() {
        synchronized (this) {
            System.out.println(Thread.currentThread().getName() + "：begin....");
            try {
                Thread.sleep(6000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "：syncRunnable3....");
        }
    }

    public void execute4() {
        synchronized (this) {
            System.out.println(Thread.currentThread().getName() + "：begin....");
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "：syncRunnable4....");
        }
    }

    public void execute5() {
        System.out.println(Thread.currentThread().getName() + "：begin....");
        synchronized (lock2) {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "：syncRunnable5....");
        }
    }

    public synchronized static void execute6(){
        System.out.println(Thread.currentThread().getName()+":begin.....");
        try {
            Thread.sleep(13000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+":syncRunnable6.....");
    }
}
